﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using ww.wwf.wwfbll;
using ww;
using System.Net;
using System.Xml;
using AutoUpdate;
using System.Configuration;

namespace ww.form
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        public static bool sfzc = false;                    //系统是否注册
        public static DateTime sys_jzrq;                       //系统注册截至日期
        public static string str_args = "";
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            AddConfig("AutoUpdate", "否");

            foreach (string str in args)
            {
                str_args = str_args + str;
            }

            string AutoUpdate = "否";
            try
            {
                AutoUpdate = System.Configuration.ConfigurationManager.AppSettings["AutoUpdate"].ToString();//是否开启自动更新
            }
            catch
            {
            }

            #region 自动更新文件
            if (AutoUpdate == "是")
            {
                AutoUpdater au = new AutoUpdater();
                try
                {
                    au.Update();
                }
                catch (WebException exp)
                {
                    MessageBox.Show(String.Format("无法找到指定资源\n\n{0}", exp.Message), "自动升级", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                catch (XmlException exp)
                {
                    MessageBox.Show(String.Format("下载的升级文件有错误\n\n{0}", exp.Message), "自动升级", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                catch (NotSupportedException exp)
                {
                    MessageBox.Show(String.Format("升级地址配置错误\n\n{0}", exp.Message), "自动升级", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                catch (ArgumentException exp)
                {
                    MessageBox.Show(String.Format("下载的升级文件有错误\n\n{0}", exp.Message), "自动升级", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                catch (Exception exp)
                {
                    MessageBox.Show(String.Format("升级过程中发生错误\n\n{0}", exp.Message), "自动升级", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            #endregion

            try
            {
                // System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("zh-CHS");
                WWFInit wwfinit = new WWFInit();
                //  bool bolOK =  wwfinit.DbClientUser();
                // MessageBox.Show(bolOK.ToString());


                LoginBLL bll = new LoginBLL();
                bll.BllSetSysInfo();
                //Application.Run(new ww.form.Form1());
                Application.Run(new wwf.MainForm());
                /*
                if (wwfinit.DbClientUser())
                {                    
                   // ww.wwf.com.Security.PwdConfiguration();
                    LoginBLL bll = new LoginBLL();
                    bll.BllSetSysInfo();
                    Application.Run(new wwf.MainForm());
                    //Application.Run(new wwf.Form1());

                }*/
            }
            catch (Exception ex)
            {
                WWMessage.MessageShowWarning("启动失败的可能原因是：\n\n" + ex.Message.ToString() + "\n\n服务支持");
            }

        }

        #region 读写配置文件

        /// <summary>
        /// 修改配置文件中某项的值
        /// </summary>
        /// <param name="key">appSettings的key</param>
        /// <param name="value">appSettings的Value</param>
        public static void AddConfig(string key, string value)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            if (config.AppSettings.Settings[key] == null)
                config.AppSettings.Settings.Add(key, value);

            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }

        /// <summary>
        /// 修改配置文件中某项的值
        /// </summary>
        /// <param name="key">appSettings的key</param>
        /// <param name="value">appSettings的Value</param>
        public static void SetConfig(string key, string value)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            if (config.AppSettings.Settings[key] != null)
                config.AppSettings.Settings[key].Value = value;
            else
                config.AppSettings.Settings.Add(key, value);

            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }

        /// <summary>
        /// 读取配置文件某项的值
        /// </summary>
        /// <param name="key">appSettings的key</param>
        /// <returns>appSettings的Value</returns>
        public static string GetConfig(string key)
        {
            string _value = string.Empty;
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (config.AppSettings.Settings[key] != null)
            {
                _value = config.AppSettings.Settings[key].Value;
            }
            return _value;
        }
        #endregion
    }
}